package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Sala;
//	En un futuro se puede poner nombre
//	En un futuro se puede poner nombre
//	En un futuro se puede poner nombre

public class SalaDAO extends JDBC {

	private static SalaDAO salaDAO;

	private SalaDAO() {
		super();
	}

	public static SalaDAO getInstance() {
		if (salaDAO == null) {
			salaDAO = new SalaDAO();
		}
		return salaDAO;
	}

	public int add(Sala sala) {
		try {

			String queryString = "INSERT INTO salas(nombre)" + " VALUES(?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setString(1, sala.getNombre());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	// En un futuro se puede poner nombre

	public boolean update(Sala sala) {
		boolean state = false;
		try {
			String queryString = "UPDATE salas SET nombre=?, capacidad=?, cantDePc=?, descripcion=? WHERE idsala=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setString(1, sala.getNombre());
			preparedStatement.setLong(2, sala.getCapacidad());
			preparedStatement.setLong(3, sala.getCantDePc());
			preparedStatement.setString(4, sala.getDescripcion());
			preparedStatement.setLong(5, sala.getId());

			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer id) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM salas WHERE idsala=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, id);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public Sala get(Integer id) {

		Sala sala = new Sala();
		try {
			String queryString = "SELECT * FROM salas where idsala = '" + id + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				sala = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return sala;
	}

	// En un futuro se puede poner nombre

	public Sala find(String nombre) {

		Sala sala = new Sala();
		try {
			String queryString = "SELECT * FROM salas where nombre ='" + nombre + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				sala = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return sala;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idsala FROM salas order by idSala DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idSala");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<Sala> getAll() {
		List<Sala> salaList = new ArrayList<Sala>();

		try {
			String queryString = "SELECT * FROM salas ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				salaList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return salaList;
	}

	private Sala populate() throws SQLException {

		Sala sala = new Sala();
		sala.setId((int) resultSet.getLong("idSala"));
		sala.setCapacidad((int) resultSet.getLong("capacidad")); 
		sala.setCantDePc((int) resultSet.getLong("cantDePc"));
		sala.setDescripcion(resultSet.getString("descripcion"));
		sala.setNombre(resultSet.getString("nombre")); // En un futuro se puede poner nombre
		return sala;

	}

}